{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "c9a148aa-b031-4a47-8185-3432b9d34e15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sentence: People have been moving back into downtown.\n",
      "shortest path lenth:  4\n",
      "shortest path:  ['people', 'moving', 'back', 'into', 'downtown']\n"
     ]
    }
   ],
   "source": [
    "import spacy\n",
    "import networkx as nx\n",
    "nlp = spacy.load(\"en_core_web_sm\")\n",
    "\n",
    "# text = u'Convulsions that occur after DTaP are caused by a fever.'\n",
    "text = u'People have been moving back into downtown.'\n",
    "# text = u'The child was carefully wrapped and bound into the cradle by means of a cord.'\n",
    "entity1 = 'People'.lower()\n",
    "entity2 = 'downtown'\n",
    "doc = nlp(text)\n",
    "\n",
    "print('sentence:',format(doc))\n",
    "# Load spacy's dependency tree into a networkx graph\n",
    "edges = []\n",
    "for token in doc:\n",
    "    for child in token.children:\n",
    "        edges.append(('{0}'.format(token.lower_),\n",
    "                      '{0}'.format(child.lower_)))\n",
    "graph = nx.Graph(edges)\n",
    "# Get the length and path\n",
    "print('shortest path lenth: ',nx.shortest_path_length(graph, source=entity1, target=entity2))\n",
    "print('shortest path: ',nx.shortest_path(graph, source=entity1, target=entity2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "0c961f54-ba1d-44ea-a284-81ccd1e7d478",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = u\"The <e1>cultivation</e1> consisted of plowing the crop with a double-shovel <e2>plow</e2>.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "3ef17cd0-50d1-458b-9785-3f307c85b3fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import re\n",
    "import spacy\n",
    "import networkx as nx\n",
    "from nltk.tokenize import word_tokenize  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "199dc27a-f1ee-46d1-b0ec-d4b7ff767275",
   "metadata": {},
   "outputs": [],
   "source": [
    "e1 = re.findall(r'<e1>(.*)</e1>', text)[0]  # 返回sentence中<e1></e1>之间的内容，返回形式是数组，[0]即实体名e1\n",
    "e2 = re.findall(r'<e2>(.*)</e2>', text)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "c2dfdbe0-d030-4901-bc46-1eb33c080e40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The cultivation consisted of plowing the crop with a double-shovel plow.'"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = text.replace('<e1>','')\n",
    "text = text.replace('</e1>','')\n",
    "text = text.replace('<e2>','')\n",
    "text = text.replace('</e2>','')\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "998beb41-3146-4508-b8e4-cb04503574b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The entity1 consisted of entity2ing the crop with a double-shovel plow.'"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = text.replace(e1, 'entity1',1)\n",
    "text = text.replace(e2, 'entity2',1)\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "15e3b10b-e5d3-4fb2-b50e-a0aa0b0152c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sentence: This process passes on a health entity1 to the next entity2.\n",
      "shortest path lenth:  4\n",
      "shortest path:  ['entity1', 'on', 'passes', 'to', 'entity2']\n"
     ]
    }
   ],
   "source": [
    "doc = nlp(text)\n",
    "\n",
    "print('sentence:',format(doc))\n",
    "# Load spacy's dependency tree into a networkx graph\n",
    "edges = []\n",
    "for token in doc:\n",
    "    for child in token.children:\n",
    "        edges.append(('{0}'.format(token.lower_),\n",
    "                      '{0}'.format(child.lower_)))\n",
    "graph = nx.Graph(edges)\n",
    "# Get the length and path\n",
    "print('shortest path lenth: ',nx.shortest_path_length(graph, source='entity1', target='entity2'))\n",
    "print('shortest path: ',nx.shortest_path(graph, source='entity1', target='entity2'))\n",
    "text = nx.shortest_path(graph, source='entity1', target='entity2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "66a0c771-c3ba-44e7-92b0-fd9214dc533d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['entity1', 'entity2']"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c9ca7c4-702e-4052-9aa9-fec7579f86a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
